package com.demoweb.flappy.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/flappy/down/gamedown.action")
public class DownLoadServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("application/octet-stream");
		resp.setHeader("Content-Disposition",	"attachment;filename=FlappyFish.zip");
		
		File file = new File("C:\\Program Files\\FlappyFIsh\\FlappyFish.zip");
		FileInputStream fileIn = new FileInputStream(file);
		ServletOutputStream out = resp.getOutputStream();
		 
		byte[] outputByte = new byte[4096];
		//copy binary contect to output stream
		while(fileIn.read(outputByte, 0, 4096) != -1)
		{
			out.write(outputByte, 0, 4096);
		}
		fileIn.close();
		out.flush();
		out.close();
		
		RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/views/flappy/download/gamedownload.jsp");

		dispatcher.forward(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doGet(req, resp);
		
	}
}